package com.ideaction.game.utils;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * 类说明：时间功能函数库
 */

public final class TimeKit {

    /* static fields */
    public static final String EMPTY = "";
    /**
     * 年月日, 时分秒 格式
     */
    public static String FORMAT_YMD_HMS = "yyyy-MM-dd HH:mm:ss", FORMAT_YMD = "yyyy-MM-dd",
            FORMAT_YMD_HM = "yyy-MM-dd HH:mm", FORMAT_YMDHMS = "yyyyMMddHHmmss";
    /**
     * 库信息
     */
    public static final String toString = TimeKit.class.getName();
    /**
     * 星期
     */
    public static final String[] WEEK = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
            "Saturday"};
    /**
     * 星期简写
     */
    public static final String[] WEEK_ = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    /**
     * 月
     */
    public static final String[] MONTH = {"January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December"};
    /**
     * 月简写
     */
    public static final String[] MONTH_ = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
            "Dec"};
    public static final String ISODATE = "ISODate";
    /**
     * 一天的时间(秒)
     */
    public static final int DAY_SECOND = 24 * 60 * 60;

    /**
     * 时区
     */
    public static String timeZoneID;

    /**
     * 时间的修正值
     */
    private static long timeFix;

    /**
     * 时区
     */
    private static TimeZone timeZone;

    public static void setTimeZone(TimeZone tz) {
        timeZone = tz;
    }

    public static TimeZone getTimeZone() {
        if (timeZone == null)
            timeZone = TimeZone.getTimeZone(System.getProperty("timeZoneID"));
        return timeZone;
    }

    /* static methods */

    /**
     * 得到校正后时间，毫秒为单位
     */
    public static long getMillisTime() {
        return System.currentTimeMillis() - timeFix;
    }

    /**
     * 得到校正后时间，秒为单位
     */
    public static int getSecondTime() {
        return (int) ((System.currentTimeMillis() - timeFix) / 1000);
    }

    /**
     * 校正时间
     */
    public static void resetTime(long time) {
        timeFix = System.currentTimeMillis() - time;
    }

    /**
     * 将指定的毫秒数转换成秒数，毫秒数除1000
     */
    public static int timeSecond(long timeMillis) {
        return (int) (timeMillis / 1000);
    }

    /**
     * 将指定的秒数转换成毫秒数，秒数乘1000
     */
    public static long timeMillis(long timeSecond) {
        return timeSecond * 1000;
    }

    /**
     * 获得传入时间的格式yyyy-MM-dd HH:mm:ss time为毫秒
     */
    public static String getFormatDate(long time) {
        return getFormatDate(time, FORMAT_YMD_HMS);
    }

    /**
     * 获得传入时间的格式yyyy-MM-dd time为毫秒
     */
    public static String getYMDFormatDate(int time) {
        return getYMDFormatDate((long) time * 1000);
    }

    /**
     * 当前时间
     */
    public static String getCurrentYMDFormatDate() {
        return getYMDFormatDate(getMillisTime());
    }

    /**
     * 获得传入时间的格式yyyy-MM-dd time为毫秒
     */
    public static String getYMDFormatDate(long time) {
        return getFormatDate(time, FORMAT_YMD);
    }

    public static String getFormatDate(long time, String format) {
        if (time == 0)
            return EMPTY;
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(time);
        SimpleDateFormat df = new SimpleDateFormat(format, new DateFormatSymbols());
        df.setTimeZone(getTimeZone());
        String timeStr = df.format(calendar.getTime());
        return timeStr;
    }

    /**
     * 获得传入时间的格式yyyy-MM-dd HH:mm:ss time为毫秒
     */
    public static String getFormatDate(int time) {
        return getFormatDate((long) time * 1000);
    }

    /**
     * 获得传入时间的格式yyyy-MM-dd HH:mm:ss time为毫秒 ( 此方法为打印调用方法, 并不用于逻辑 )
     */
    public static String printFormatDate(int time) {
        return time + "(" + getFormatDate((long) time * 1000) + ")";
    }

    /**
     * 当前时间
     */
    public static String getCurrentFormatDate() {
        return getFormatDate(getSecondTime());
    }

    /**
     * 根据 yyyy-MM-dd 日期获得int时间
     */
    public static int getTimeFromYMD(String ymd) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_YMD, new DateFormatSymbols());
        df.setTimeZone(getTimeZone());
        Date date;
        try {
            date = df.parse(ymd);
            return (int) (date.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 根据 yyyy-MM-dd hh:mm:ss 日期获得int时间
     */
    public static int getTimeFromYMDHMS(String ymdhms) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_YMD_HMS, new DateFormatSymbols());
        df.setTimeZone(getTimeZone());
        Date date;
        try {
            date = df.parse(ymdhms);
            return (int) (date.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 根据 yyyy-MM-dd 日期获得int时间
     */
    public static String getNextDayFromYMD(String ymd) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_YMD, new DateFormatSymbols());
        df.setTimeZone(getTimeZone());
        Date date;
        try {
            date = df.parse(ymd);
            Calendar calendar = new GregorianCalendar();
            calendar.setTimeInMillis(date.getTime());

            calendar.add(Calendar.DAY_OF_YEAR, 211);
            return df.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获得某个时间点所在那天的零点时间
     */
    public static int getZeroTime(int time) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeZone(getTimeZone());
        calendar.setTimeInMillis((long) time * 1000);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return (int) (calendar.getTimeInMillis() / 1000);
    }

    /**
     * 获取当前时间格式（充值专用）
     */
    public static String getTimeYMDHS(long time) {
        time += getMillisTime();
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(time);
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_YMDHMS, new DateFormatSymbols());
        df.setTimeZone(getTimeZone());
        String timestr = df.format(calendar.getTime());
        return timestr;
    }

    /**
     * 获得当天的零点时间
     */
    public static int getCurrentZeroTime() {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeZone(getTimeZone());
        calendar.setTimeInMillis(getMillisTime());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return (int) (calendar.getTimeInMillis() / 1000);
    }

    /**
     * 根据秒数获得时间(天 时 分 秒)
     */
    public static String getTimeBySecond(int time) {
        String timeStr = null;
        int day = 0;
        int hour = 0;
        int minute = 0;
        int second = time % 60;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 24) {
                    // day
                    day = hour / 24;
                    hour = hour % 24;
                }
                minute = minute % 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
                if (day > 0) {
                    timeStr = day + " " + timeStr;
                }
            }
        }
        return timeStr;
    }

    /**
     * 计算两个时间相差多少天
     */
    public static int getIntervalDays(int time1, int time2) {
        int zero1 = getZeroTime(time1);
        int zero2 = getZeroTime(time2);
        return (int) (Math.abs(zero1 - zero2) / 86400);
    }

    /**
     * 根据秒数获得时间字符串( 满足天的, 则只返回天:时 ;满足时的, 则返回 时:分 ;只满足分的, 则返回 分:秒 )
     */
    public static String getTwoDigitTimeBySecond(int time) {
        if (time <= 0)
            return "0";
        String timeStr = null;
        int day = 0;
        int hour = 0;
        int minute = 0;
        int second = time % 60;

        minute = time / 60;
        // 不超过1小时
        if (minute < 60) {
            timeStr = minute + "m" + second + "s";
        } else {
            hour = minute / 60;
            if (hour > 24) {
                // day
                day = hour / 24;
                hour = hour % 24;
            }
            minute = minute % 60;
            timeStr = hour + "h" + minute + "m";
            if (day > 0) {
                timeStr = day + "d" + hour + "h";
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    /* constructors */
    private TimeKit() {
    }

}